package com.student.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.student.Activitys.bean.UserBean;
import com.student.Activitys.bean.pack.SupportMyApplyJsonPack;
import com.student.Activitys.http.ApiService;
import com.student.Activitys.http.NetWorkMgr;
import com.student.Activitys.http.api.MyAPI;
import com.student.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 项目名称：
 * 类描述：我的 （个人信息、登陆） 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2017/9/27 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2017/9/27 0027 下午 1:33
 * 修改备注：
 */
public class MyManager {

    private static MyAPI mMyAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType = Constant.FIRST_LOAD;

    private static class MyApiManagerHolder {
        private static final MyManager INSTANCE = new MyManager();
    }
    public static final MyManager getMyApiManager(Activity activity, int taskId) {
        mMyAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(MyAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return MyApiManagerHolder.INSTANCE;
    }
    public void getType(int type){
        RefType=type;
    }


    /**
     *
     * 登陆 请求
     *
     */
    public void login(String action,String xh,String pwd,String stuType) {
        Subscription subscription = mMyAPI
                .login(action,xh,pwd,stuType).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBean>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_LOGIN_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"login(登陆) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(UserBean user) {
                        //返回数据处理
                        if (user != null)
                            EventBus.getDefault().post(user, Constant.BUS_LOGIN);
                        else
                            Log.e(mActivity.getClass().getSimpleName(),"数据源为空");
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     *
     * 我的 奖惩申请
     *
     */
    public void supportMyApplyList(String action,String xh,int index,int pageCount) {
        Subscription subscription = mMyAPI
                .supportMyApplyList(action,xh,index,pageCount).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SupportMyApplyJsonPack>() {
                    @Override
                    public void onCompleted() {
                        //加载完成,可用于展示进度条
                        EventBus.getDefault().post("onCompleted", Constant.BUS_MY_SUPPORT_APPLY_ON_COMPLETED);
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"supportMyApplyList(我的奖惩申请) 错误" + e.toString());
                    }
                    @Override
                    public void onNext(SupportMyApplyJsonPack pack) {
                        //返回数据处理
                        if (pack != null) {
                            if(RefType== Constant.FIRST_LOAD){
                                EventBus.getDefault().post(pack, Constant.BUS_MY_SUPPORT_APPLY_LIST);
                            }else{
                                EventBus.getDefault().post(pack, Constant.BUS_MY_SUPPORT_APPLY_FOOT_NOTICE);
                            }
                        }else{
                            Log.e(mActivity.getClass().getSimpleName(), "数据源为空");
                        }
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }
}
